<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/11
 * Time: 11:25
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class AttendanceModel
{
    const TABLE = 'attendance';

    /**
     * @param $id
     * @param $startTime
     * @param $endTime
     * @return mixed
     */
    public function getAttendancesSum($id, $startTime, $endTime)
    {
        return DB::table(self::TABLE)
            ->where('employeeId',$id)
            ->where('day','>=',$startTime)
            ->where('day','<=',$endTime)
            ->select(DB::raw('SUM(length) as attendance'))
            ->first();
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function getLists(array $data)
    {
        return DB::table(self::TABLE)
            ->leftJoin('project', 'project.id', '=', self::TABLE . '.projectId')
            ->where('employeeId',$data['employeeId'])
            ->where(function($query) use($data){
                if(!empty($data['projectId'])){
                    $query->where(self::TABLE.'.projectId',$data['projectId']);
                }
            })
            ->whereBetween('day',[$data['startTime'],$data['endTime']])
            ->select(self::TABLE.'.id',self::TABLE.'.projectId',self::TABLE.'.day',self::TABLE.'.length',self::TABLE.'.status',self::TABLE.'.isCurrent','project.name as projectName')
            ->get()->toArray();
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function insert(array $data)
    {
        if (count($data) == count($data,1)){
            $data['createTime'] = date('Y-m-d H:i:s');
            $d = [
                'employeeId'=>$data['employeeId'],
                'projectId'=>$data['projectId'],
                'day'=>$data['day'],
            ];
            DB::table(self::TABLE)->where($d)->delete();
            return DB::table(self::TABLE)->insert($data);
        }else{
            foreach ($data['data'] as $key => $d){
                $data['data'][$key]['createTime'] = date('Y-m-d H:i:s');
                $dd = [
                    'employeeId'=>$d['employeeId'],
                    'projectId'=>$d['projectId'],
                    'day'=>$d['day'],
                ];
                DB::table(self::TABLE)->where($dd)->delete();
            }
            return DB::table(self::TABLE)->insert($data['data']);
        }
    }

    /**
     * @param array $data
     * @return array
     */
    public function info(array $data)
    {
        $result = DB::table(self::TABLE)->where($data)->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function getSum(array $data)
    {
        return DB::table(self::TABLE)
            ->leftJoin('employee as e','e.id','=',self::TABLE.'.employeeId')
            ->where(function($query)use($data){
                if (!empty($data['projectId'])){
                    $query->where(self::TABLE.'.projectId',$data['projectId']);
                }
                if (!empty($data['professionId'])){
                    $query->where('e.professionId',$data['professionId']);
                }
                if (!empty($data['startTime'])){
                    $query->where('day','>=',$data['startTime']);
                }
                if (!empty($data['endTime'])){
                    $query->where('day','<=',$data['endTime']);
                }
                if (!empty($data['employeeId'])){
                    $query->where('employeeId',$data['employeeId']);
                }
                if (isset($data['status'])){
                    if (!is_array($data['status'])){
                        $query->where(self::TABLE.'.status',$data['status']);
                    }else{
                        $query->whereIn(self::TABLE.'.status',$data['status']);
                    }
                }
            })->sum('length');
    }

    public function update(array $where, array $data)
    {
        return DB::table(self::TABLE)
            ->where(function($query)use($where){
                if (!empty($where['id'])){
                    $query->where('id',$where['id']);
                }
                if (!empty($where['projectId'])){
                    $query->where('projectId',$where['projectId']);
                }
                if (!empty($where['employeeId'])){
                    $query->where('employeeId',$where['employeeId']);
                }
                if (!empty($where['status'])){
                    $query->where('status',$where['status']);
                }
                if (!empty($where['startTime']) && !empty($where['endTime'])){
                    $query->whereBetween('day',[$where['startTime'],$where['endTime']]);
                }
            })
            ->update($data);
    }

    public function delete(array $param)
    {
        return DB::table(self::TABLE)->where($param)->delete();
    }
}